@@ -2,6 +2,7 @@ require 'ruby-growl' |
||
2 | 2 |
|
3 | 3 |
module Agents |
4 | 4 |
class GrowlAgent < Agent |
5 |
+ attr_reader :growler |
|
5 | 6 |
|
6 | 7 |
cannot_be_scheduled! |
7 | 8 |
cannot_create_events! |
@@ -45,8 +46,8 @@ module Agents |
||
45 | 46 |
end |
46 | 47 |
|
47 | 48 |
def receive(incoming_events) |
49 |
+ register_growl |
|
48 | 50 |
incoming_events.each do |event| |
49 |
- register_growl |
|
50 | 51 |
message = (event.payload['message'] || event.payload['text']).to_s |
51 | 52 |
subject = event.payload['subject'].to_s |
52 | 53 |
if message.present? && subject.present? |
@@ -5,15 +5,13 @@ describe Agents::GrowlAgent do |
||
5 | 5 |
@checker = Agents::GrowlAgent.new(:name => 'a growl agent', |
6 | 6 |
:options => { :growlserver => 'localhost', |
7 | 7 |
:growlappname => 'HuginnGrowlApp', |
8 |
+ :growlpassword => 'mypassword', |
|
8 | 9 |
:growlnotificationname => 'Notification', |
9 | 10 |
:expected_receive_period_in_days => '1' }) |
10 | 11 |
@checker.user = users(:bob) |
11 | 12 |
@checker.save! |
12 | 13 |
|
13 |
- class Agents::GrowlAgent #this should really be done with RSpec-Mocks |
|
14 |
- def notify_growl(message,subject) |
|
15 |
- end |
|
16 |
- end |
|
14 |
+ stub.any_instance_of(Growl).notify |
|
17 | 15 |
|
18 | 16 |
@event = Event.new |
19 | 17 |
@event.agent = agents(:bob_weather_agent) |
@@ -47,4 +45,72 @@ describe Agents::GrowlAgent do |
||
47 | 45 |
@checker.should_not be_valid |
48 | 46 |
end |
49 | 47 |
end |
48 |
+ |
|
49 |
+ describe "register_growl" do |
|
50 |
+ it "should set the password for the Growl connection from the agent options" do |
|
51 |
+ @checker.register_growl |
|
52 |
+ @checker.growler.password.should eql(@checker.options[:growlpassword]) |
|
53 |
+ end |
|
54 |
+ |
|
55 |
+ it "should add a notification to the Growl connection" do |
|
56 |
+ any_instance_of(Growl) do |obj| |
|
57 |
+ mock(obj).add_notification(@checker.options[:growlnotificationname]) |
|
58 |
+ end |
|
59 |
+ |
|
60 |
+ @checker.register_growl |
|
61 |
+ end |
|
62 |
+ end |
|
63 |
+ |
|
64 |
+ describe "notify_growl" do |
|
65 |
+ before do |
|
66 |
+ @checker.register_growl |
|
67 |
+ end |
|
68 |
+ |
|
69 |
+ it "should call Growl.notify with the correct notification name, subject, and message" do |
|
70 |
+ message = "message" |
|
71 |
+ subject = "subject" |
|
72 |
+ any_instance_of(Growl) do |obj| |
|
73 |
+ mock(obj).notify(@checker.options[:growlnotificationname],subject,message) |
|
74 |
+ end |
|
75 |
+ @checker.notify_growl(subject,message) |
|
76 |
+ end |
|
77 |
+ end |
|
78 |
+ |
|
79 |
+ describe "receive" do |
|
80 |
+ def generate_events_array |
|
81 |
+ events = [] |
|
82 |
+ (2..rand(7)).each do |
|
83 |
+ events << @event |
|
84 |
+ end |
|
85 |
+ return events |
|
86 |
+ end |
|
87 |
+ |
|
88 |
+ it "should call register_growl once regardless of number of events received" do |
|
89 |
+ mock.proxy(@checker).register_growl.once |
|
90 |
+ @checker.receive(generate_events_array) |
|
91 |
+ end |
|
92 |
+ |
|
93 |
+ it "should call notify_growl one time for each event received" do |
|
94 |
+ events = generate_events_array |
|
95 |
+ events.each do |event| |
|
96 |
+ mock.proxy(@checker).notify_growl(event.payload['subject'],event.payload['message']) |
|
97 |
+ end |
|
98 |
+ @checker.receive(events) |
|
99 |
+ end |
|
100 |
+ |
|
101 |
+ it "should not call notify_growl if message or subject are missing" do |
|
102 |
+ event_without_a_subject = Event.new |
|
103 |
+ event_without_a_subject.agent = agents(:bob_weather_agent) |
|
104 |
+ event_without_a_subject.payload = { :message => 'Looks like its going to rain' } |
|
105 |
+ event_without_a_subject.save! |
|
106 |
+ |
|
107 |
+ event_without_a_message = Event.new |
|
108 |
+ event_without_a_message.agent = agents(:bob_weather_agent) |
|
109 |
+ event_without_a_message.payload = { :subject => 'Weather Alert YO!' } |
|
110 |
+ event_without_a_message.save! |
|
111 |
+ |
|
112 |
+ mock.proxy(@checker).notify_growl.never |
|
113 |
+ @checker.receive([event_without_a_subject,event_without_a_message]) |
|
114 |
+ end |
|
115 |
+ end |
|
50 | 116 |
end |